package com.soft.service.impl;

import java.util.List;

import com.soft.dao.VillaDao;
import com.soft.model.Villa;
import com.soft.service.IVillaService;
import com.soft.util.Const;

public class VillaServiceImpl implements IVillaService {

	@Override
	public boolean add(Villa villa) {
		String sql = "insert into " + Const.VILLA_TB
				+ " (villaName, villaDescription, villaPrice, storeId, imgPath,addTime) values (?,?,?,?,?,now())";
		String villaName = villa.getVillaName();
		String villaDescription = villa.getVillaDescription();
		double villaPrice = villa.getVillaPrice();
		int storeId = villa.getStoreId();
		String imgPath = villa.getImgPath();
		return VillaDao.update(sql, villaName, villaDescription, villaPrice, storeId, imgPath);
	}

	@Override
	public boolean delete(String id) {
		String sql = "delete from " + Const.VILLA_TB + " where id=?";
		return VillaDao.update(sql, id);
	}

	@Override
	public List<Villa> findAll() {
		String sql = "select id, villaName, addTime, villaDescription, villaPrice, storeId, imgPath from " + Const.VILLA_TB + " order by addTime desc";
		return VillaDao.getVilla(sql);
	}

	@Override
	public List<Villa> find(String villaName, String storeId, String beginTime, String endTime, int pageSize , String currPage) {
		String sql = "select id, villaName, addTime, villaDescription, villaPrice, storeId, imgPath from " + Const.VILLA_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (villaName != null && !"".equals(villaName)) {
			sb.append(" and villaName like '%" + villaName + "%'");
		}
		if (storeId!=null && !"".equals(storeId) && !"0".equals(storeId)) {
			sb.append(" and storeId ="+storeId);
		}
		if (beginTime != null && !"".equals(beginTime)) {
			sb.append(" and addtime >='" + beginTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sb.append(" and addtime <='" + endTime + "'");
		}
		int pageNum = 1;
		if (currPage != null && !"".equals(currPage)) {
			pageNum=Integer.parseInt(currPage);
		}
		sb.append(" order by addTime desc");
		sb.append(" limit ?,?");
		sql = sb.toString();
//		System.out.println(sql);
		int start = (pageNum - 1) * pageSize ;
		return VillaDao.getVilla(sql, start, pageSize);
	}

	@Override
	public Villa findById(String id) {
		String sql = "select id, villaName, addTime, villaDescription, villaPrice, storeId, imgPath from " + Const.VILLA_TB + " where id=?";
		List<Villa> villa_list = VillaDao.getVilla(sql, id);
		if (villa_list != null && villa_list.size() > 0) {
			return villa_list.get(0);
		}
		return null;
	}

	@Override
	public Villa findByName(String villaName) {
		String sql = "select id, villaName, addTime, villaDescription, villaPrice, storeId, imgPath from " + Const.VILLA_TB + " where villaName=?";
		List<Villa> villa_list = VillaDao.getVilla(sql, villaName);
		if (villa_list != null && villa_list.size() == 1) {
			return villa_list.get(0);
		}
		return null;
	}

	@Override
	public boolean update(Villa villa) {
		int id = villa.getId();
		String villaName = villa.getVillaName();
		String villaDescription = villa.getVillaDescription();
		double villaPrice = villa.getVillaPrice();
		int storeId = villa.getStoreId();
		String imgPath = villa.getImgPath();
		String sql = "update " + Const.VILLA_TB + " set villaName=?, villaDescription=?, villaPrice=?, storeId=?, imgPath=?  where id=?";
		return VillaDao.update(sql, villaName, villaDescription, villaPrice, storeId, imgPath, id);
	}

	@Override
	public int getCount(String villaName, String storeId, String beginTime, String endTime) {
		String sql = "select count(id) from " + Const.VILLA_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (villaName != null && !"".equals(villaName)) {
			sb.append(" and villaName like '%" + villaName + "%'");
		}
		if (storeId!=null && !"".equals(storeId) && !"0".equals(storeId)) {
			sb.append(" and storeId ="+storeId);
		}
		if (beginTime != null && !"".equals(beginTime)) {
			sb.append(" and addtime >='" + beginTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sb.append(" and addtime <='" + endTime + "'");
		}
		sql = sb.toString();
//		System.out.println(sql);
		return VillaDao.getCount(sql);
	}

	@Override
	public int getPageCount(String villaName, String storeId, String beginTime, String endTime,int pageSize) {
		int count = getCount(villaName, storeId, beginTime, endTime);
		int pageCount = count%pageSize==0 ? count/pageSize : count/pageSize+1;
		return pageCount;
	}
}
